/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.stress.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.StressConstant;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.stress.entity.Channel;
import org.springblade.modules.stress.entity.Position;
import org.springblade.modules.stress.mapper.PositionMapper;
import org.springblade.modules.stress.service.IChannelService;
import org.springblade.modules.stress.service.IPositionService;
import org.springblade.modules.stress.vo.PositionVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 测点管理信息表 服务实现类
 *
 * @author Yanzujian
 * @since 2021-01-10
 */
@Service
@AllArgsConstructor
public class PositionServiceImpl extends BaseServiceImpl<PositionMapper, Position> implements IPositionService {
	private final IChannelService channelService;

	@Override
	public IPage<PositionVO> selectPositionPage(IPage<PositionVO> page, PositionVO position) {
		return page.setRecords(baseMapper.selectPositionPage(page, position));
	}

    @Override
    public Integer getDuplicationSensor(Long sensorId) {
        return baseMapper.getDuplicationSensor(sensorId);
    }

	@Override
	@Transactional
	public boolean savePosition(Position position) {
		boolean re = save(position);
		if (re){
			if (position.getId() > 0){
				//测点
				Channel channel = new Channel();
				channel.setChannelId(position.getId());
				channel.setParentId(position.getDeviceId());
				channel.setCode(position.getId().toString());
				channel.setName(position.getLocal());
				channel.setSensorType(StressConstant.STRESS_SENSOR_TYPE_POSITION);
				channel.setIsDeleted(BladeConstant.DB_NOT_DELETED);
				channelService.save(channel);

				return true;
			}
		}
		return false;
	}

	@Override
	public boolean updatePosition(Position position) {
		boolean re = saveOrUpdate(position);
		if (re){
			//测点
			Channel channel = channelService.getOne(Wrappers.<Channel>query().lambda().eq(Channel::getChannelId, position.getId()));
			channel.setParentId(position.getDeviceId());
			channel.setCode(position.getId().toString());
			channel.setName(position.getLocal());
			channelService.saveOrUpdate(channel);

			return true;
		}
		return false;
	}

	@Override
	@Transactional
	public boolean removePositionByIds(String ids) {
		//1、删除关联树
		channelService.remove(Wrappers.<Channel>query().lambda().eq(Channel::getSensorType, StressConstant.STRESS_SENSOR_TYPE_POSITION).in(Channel::getChannelId, Func.toLongList(ids)));
		//2、删除本体
		return removeByIds(Func.toLongList(ids));
	}

}
